<?php

class SJB_Classifieds_MakePriority extends SJB_Function
{
	public function execute()
	{
		if (isset($_REQUEST['invoice_sid'])) {

			$invoice_sid = SJB_Request::getVar("invoice_sid", null, 'default', 'int');
			$invoice = SJB_InvoiceManager::getObjectBySID($invoice_sid);

			if (!is_null($invoice)) {
				$status = $invoice->getStatus();
				if ($status == SJB_Invoice::INVOICE_STATUS_VERIFIED) {
					$items_info = $invoice->getPropertyValue('items');
					$listing_id = $items_info['custom_info'][1]['listing_id'];
					SJB_ListingManager::makePriorityBySID($listing_id);
					$invoice->setStatus(SJB_Invoice::INVOICE_STATUS_PAID);
					SJB_InvoiceManager::saveInvoice($invoice);
				}
				else {
					if ($status == SJB_Invoice::INVOICE_STATUS_PAID)
						$errors['INVOICE_IS_COMPLETED'] = 1;
					else
						$errors['INVOICE_IS_NOT_VERIFIED'] = 1;
				}

			}
			else
				$errors['INVALID_INVOICE_ID'] = 1;

		}
		else {
			$listing_id = SJB_Request::getVar("listing_id", null, 'default', 'int');
			if ($listing_id) {
				$listing = SJB_ListingManager::getObjectBySID($listing_id);
				if (!is_null($listing) && !$listing->isPriority()) {
					$productInfo = $listing->getProductInfo();
					if ($productInfo['priority'])
						SJB_ListingManager::makePriorityBySID($_REQUEST['listing_id']);
					else {
						$user_sid = $listing->getUserSID();
						$items['products'][1]  = '-1';
						$subTotalPrice = $productInfo['upgrade_to_priority_listing_price'];
						$items['price'][1] = sprintf('%.02f', $subTotalPrice);
						$items['amount'][1] = sprintf('%.02f', $subTotalPrice);
						$items['custom_item'][1] = 'Payment for upgrade listing ID' . " $listing_id " . 'to priority';
						$items['custom_info'][1] =  array('listing_id' => $listing_id, 'type' => 'priorityListing');
						$items['qty'][1] = '1';
						$tax_info = SJB_TaxesManager::getTaxInfoByUserSidAndPrice($user_sid, $subTotalPrice);
						$totalPrice = SJB_InvoiceManager::getTotalPrice($subTotalPrice, $tax_info['tax_amount'], $tax_info['price_includes_tax']);
						$invoice_info = array(
							'user_sid' => $user_sid,
							'include_tax' => !empty($tax_info['tax_info']['tax_name']) ? 1 : 0,
							'total' => $totalPrice,
							'sub_total' => $subTotalPrice,
							'success_page_url' =>SJB_System::getSystemSettings('SITE_URL') . "/make-priority/",
							'status' => SJB_Invoice::INVOICE_STATUS_UNPAID,
							'tax_info' => $tax_info,
							'items' => $items,
							'is_recurring' => false
						);
						$invoice_sid = SJB_InvoiceManager::generateInvoice($invoice_info);
						$payment_page_url = SJB_System::getSystemSettings('SITE_URL') . "/payment-page/?invoice_sid=" . $invoice_sid;
						SJB_HelperFunctions::redirect($payment_page_url);
						return;
					}
				} elseif (is_null($listing)) {
					$errors['INVALID_LISTING_ID'] = 1;
				} else {
					$errors['LISTING_ALREADY_PRIORITY'] = 1;
				}

			} else {
				$errors['PARAMETERS_MISSED'] = 1;
			}
		}

		$tp = SJB_System::getTemplateProcessor();
		$tp->assign("errors", (isset($errors) ? $errors : null));
		$tp->display("make_listing_priority.tpl");
	}
}
